package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.constant.UpmpConstant;
import cc.rengu.redp.bizimpl.enums.*;
import cc.rengu.redp.bizimpl.igas.service.PTxnCfgService;
import cc.rengu.redp.bizimpl.igas.vo.PTxnAuthMap;
import cc.rengu.redp.bizimpl.igas.vo.PTxnGroup;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.dao.BBaseProdInfoMapper;
import cc.rengu.redp.bizimpl.upmp.domain.*;
import cc.rengu.redp.bizimpl.upmp.service.*;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.common.utils.DateUtil;
import cc.rengu.redp.common.utils.SortUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 基础产品管理 服务实现类
 * </p>
 *
 * @author 刘玉亮.LL
 * @since 2021-06-10
 */
@Service
public class BBaseProdInfoServiceImpl extends ServiceImpl<BBaseProdInfoMapper, BBaseProdInfo> implements IBBaseProdInfoService {
    @Autowired
    private IBBaseProdInfoService bBaseProdInfoService;
    @Autowired
    private IBProdFeeInfoService bProdFeeInfoService;
    @Autowired
    private PTxnCfgService pTxnCfgService;
    @Autowired
    private IBProdAuthCtrlService bProdAuthCtrlService;
    @Autowired
    private IBProdStageInfoService bProdStageInfoService;
    @Autowired
    private IUpmpSysParamService upmpSysParamService;
    @Autowired
    private IBizTmpService bizTmpService;
    @Autowired
    private IBizHisService bizHisService;
    @Autowired
    IUpmpPrivilegeCheckService privilegeCheckService;
    private String oprMenuId = "20000016";
    private String oprMenuName = "产品管理:基础产品管理";
    private String oprBtnNamePrefix = "基础产品管理";
    private String webNextUrl = "/admin/mcmp/bBaseProdInfo/edit";
    @Override
    public String getOprMenuId() {
        return oprMenuId;
    }
    @Override
    public String getOprMenuName() {
        return oprMenuName;
    }
    @Override
    public String getWebNextUrl() {
        return webNextUrl;
    }
    @Override
    public String getOprBtnNamePrefix() {
        return oprBtnNamePrefix;
    }
    @Override
    public IPage<BBaseProdInfo> findByPage(QueryRequest request, BBaseProdInfo info) {
        try {
            LambdaQueryWrapper<BBaseProdInfo> queryWrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotBlank(info.getInstId())) {
                queryWrapper.eq(BBaseProdInfo::getInstId, info.getInstId());
            } else {
                //获取当前用户的法人id
                UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
                queryWrapper.eq(BBaseProdInfo::getInstId, user.getInstId());
            }
            if(StringUtils.isNotBlank(info.getBaseProdCode())){
                queryWrapper.like(BBaseProdInfo::getBaseProdCode,info.getBaseProdCode());
            }
            if(StringUtils.isNotBlank(info.getBaseProdName())){
                queryWrapper.like(BBaseProdInfo::getBaseProdName,info.getBaseProdName());
            }
            queryWrapper.ne(BBaseProdInfo::getBaseProdCode, "00");
            Page<BBaseProdInfo> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            return this.page(page, queryWrapper);
        } catch (Exception e) {
            log.error("查询失败", e);
            return null;
        }
    }
    @Override
    public List<BBaseProdInfo> findByBaseProdCode(String instId ,String prodStatus, String baseProdCode) {
        try {
            LambdaQueryWrapper<BBaseProdInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BBaseProdInfo::getInstId, instId);
            queryWrapper.eq(BBaseProdInfo::getProdStatus, prodStatus);
            queryWrapper.ne(BBaseProdInfo::getBaseProdCode, baseProdCode);
            return this.list(queryWrapper);
        } catch (Exception e) {
            log.error("查询失败", e);
            return null;
        }
    }
    @Override
    @Transactional
    public void update(BBaseProdInfo entity) throws RedpException {
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        //判空
        if (entity.getBProdFeeList() == null) {
            entity.setBProdFeeList(new ArrayList<>());
        }
        if (entity.getBProdStageList() == null) {
            entity.setBProdStageList(new ArrayList<>());
        }
        if (entity.getBProdAuthCtrlList() == null) {
            entity.setBProdAuthCtrlList(new ArrayList<>());
        }

        LambdaQueryWrapper<BBaseProdInfo> queryWrapper = new LambdaQueryWrapper<>();
        BBaseProdInfo bBaseProdInfo;
        if (StringUtils.isNotBlank(entity.getId())) {
            QueryWrapper queryWrapper1=new QueryWrapper();
            queryWrapper1.eq("ID",entity.getId());
            queryWrapper1.eq("INST_ID",entity.getInstId());
            bBaseProdInfo=    bBaseProdInfoService.getOne(queryWrapper1);
          //  bBaseProdInfo = bBaseProdInfoService.getById(entity.getId());
        } else {
            queryWrapper.eq(BBaseProdInfo::getInstId, entity.getInstId());
            queryWrapper.eq(BBaseProdInfo::getBaseProdCode, entity.getBaseProdCode());
            bBaseProdInfo = bBaseProdInfoService.getOne(queryWrapper);
        }
        String instId = user.getInstId();
        // 获取当前登录用户信息
        String lastUpddt = DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN);
        entity.setLastUpdateTime(lastUpddt);
        if (PageOptFlagEnum.OPT_FLAG_ADD.getType().equals(entity.getOptFlag())) {
            throw new RedpException("基础产品不支持新增");
        } else if (PageOptFlagEnum.OPT_FLAG_UPDATE.getType().equals(entity.getOptFlag())) {
            UpmpPrivilegeCheck privilegeCheck = privilegeCheckService.getPrivilegeCheckByPrivilegeCode(instId,
                    oprBtnNamePrefix + BusinessContainer.AUTH_PRIVILEGE_CODE_SPLIT + PageOptFlagEnum.OPT_FLAG_UPDATE.getDesc());
            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                // 修改
                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                entity.setLastOprId(user.getId());
                BeanUtils.copyProperties(entity, bBaseProdInfo);
                if (Common.isBlank(bBaseProdInfo)) {
                    entity.setProdStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                    this.save(bBaseProdInfo);
                }
                //插入临时数据表
                bizTmpService.saveBizTmp(bBaseProdInfo, user, instId, BizOprEnum.OPR_UPDATE.getType(),DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN)
                        , oprMenuId, oprMenuName, bBaseProdInfo.getProdStatus(), bBaseProdInfo.getAuditStatus(), String.valueOf(bBaseProdInfo.getId()), webNextUrl, null, null, null);
                String now = DateUtils.formatDate(new Date(), DateUtils.FORMAT_YYYYMMDD);

                if (!BaseProdCodeEnum.CREDIT_CARD_STAGE_PROD.getType().equals(entity.getBaseProdCode())) {//除了信用卡分期其他都单独设置费率
                    //界面操作时费率只有修改按钮没有删除按钮，不需要判断是否有需要删除的数据
                    for (BProdFeeInfo bProdFeeInfo : entity.getBProdFeeList()) {
                        BProdFeeInfo bProdFeeDB =
                                bProdFeeInfoService.getEffectProdFee(bProdFeeInfo.getInstId(), bProdFeeInfo.getProdType(), bProdFeeInfo.getBaseProdCode(), bProdFeeInfo.getProdCode(), bProdFeeInfo.getFeeType(), Integer.valueOf(now));

                        if (Common.isBlank(bProdFeeDB)) {//如果数据库中查询不到则初始化
                            bProdFeeDB = new BProdFeeInfo();
                            bProdFeeInfo.setFeeEffectiveDate(now);
                            bProdFeeInfo.setFeeExpiryDate(BusinessContainer.doomsday);
                            bProdFeeInfo.setFeeStatus(entity.getProdStatus());
                            bProdFeeInfo.setAuditStatus(entity.getAuditStatus());
                            bProdFeeInfo.setCreateTime(lastUpddt);
                            bProdFeeInfo.setLastOprId(user.getId());
                            bProdFeeInfo.setLastUpdateTime(lastUpddt);
                            bProdFeeInfo.setOprId(user.getId());
                            BeanUtils.copyProperties(bProdFeeInfo, bProdFeeDB);
                            bProdFeeDB.setId(null);
                            bProdFeeInfoService.save(bProdFeeDB);
                        } else {//如果数据库中查询到则只初始化状态、审计相关字段值
//                            this.detach(bProdFeeDB);
//                            bProdFeeDB.setFeeInputMode(bProdFeeInfo.getFeeInputMode());
//                            bProdFeeDB.setFeeRate(bProdFeeInfo.getFeeRate());
//                            bProdFeeDB.setFeeFixedAmt(bProdFeeInfo.getFeeFixedAmt());
//                            bProdFeeDB.setFeeCappingAmt(bProdFeeInfo.getFeeCappingAmt());
//                            bProdFeeDB.setFeeEffectiveDate(now);
                            bProdFeeDB.setAuditStatus(entity.getAuditStatus());
                            bProdFeeInfo.setAuditStatus(entity.getAuditStatus());
                            bProdFeeInfo.setFeeExpiryDate(BusinessContainer.doomsday);
                            bProdFeeInfo.setFeeStatus(bProdFeeDB.getFeeStatus());
                            bProdFeeInfo.setCreateTime(lastUpddt);
                            bProdFeeInfo.setOprId(user.getId());
                            bProdFeeInfo.setLastUpdateTime(lastUpddt);
                            bProdFeeInfo.setOprId(user.getId());
                            bProdFeeInfoService.updateById(bProdFeeDB);
                        }

                        //插入临时数据表
                        bizTmpService.saveBizTmp(bProdFeeInfo, user, instId, BizOprEnum.OPR_UPDATE.getType(),DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN)
                                , oprMenuId, oprMenuName, entity.getProdStatus(), entity.getAuditStatus(), String.valueOf(bProdFeeDB.getId()), webNextUrl, null, null, null);
                    }
                }
                //界面操作时分期有修改按钮、删除按钮，需要判断是否有需要删除的数据
                if (BaseProdCodeEnum.CREDIT_CARD_STAGE_PROD.getType().equals(entity.getBaseProdCode())) {//除了信用卡分期其他都单独设置费率
                    List<BProdStageInfo> listDB = bProdStageInfoService.getBaseProdStageList(entity.getInstId(), ProdTypeEnum.BASE_PROD.getType(), entity.getBaseProdCode());
                    List<BizTmp> listTmpDBDel = Lists.newArrayList();//待删除的数据，需要审核时只从临时表删除
                    //查找提交数据中无，数据库有的数据
                    for (BProdStageInfo bProdStageDB : listDB) {
                        boolean del = true;
                        for (BProdStageInfo bProdStageInfo : entity.getBProdStageList()) {
                            if (bProdStageDB.getId().equals(bProdStageInfo.getId())) {
                                del = false;
                            }
                        }
                        if (del) {//如果需要删除则从临时表取出待删除
                            BizTmp tmp = bizTmpService.getBizTmpDetail(bProdStageDB.getId());
                            if (Common.isNotBlank(tmp)) {
                                listTmpDBDel.add(tmp);
                            }
                        }
                    }
                    if (listTmpDBDel.size() > 0) {//从临时表删除
                        bizTmpService.delete(listTmpDBDel);
                    }

                    for (BProdStageInfo bProdStageInfo : entity.getBProdStageList()) {
                        bProdStageInfo.setStagePrimaryKey(bProdStageInfo.getProdType()
                                + String.format("%2s", null == bProdStageInfo.getProdCode() ? "" : bProdStageInfo.getProdCode()) + bProdStageInfo.getBaseProdCode()
                                + String.format("%32s", bProdStageInfo.getProdStageCode())
                                + String.format("%2s", bProdStageInfo.getProdStageNum()) + bProdStageInfo.getFeeEffectiveDate());
                        BProdStageInfo bProdStageDB =
                                bProdStageInfoService.getfindbProdStageInfo(bProdStageInfo.getInstId(), bProdStageInfo.getProdType(), bProdStageInfo.getBaseProdCode(), bProdStageInfo.getProdStageCode(), bProdStageInfo.getProdStageNum());

                        if (Common.isBlank(bProdStageDB)) {//如果数据库中查询不到则初始化
                            bProdStageDB = new BProdStageInfo();
                            bProdStageInfo.setFeeEffectiveDate(now);
                            bProdStageInfo.setFeeExpiryDate(BusinessContainer.doomsday);
                            bProdStageInfo.setStageStatus(entity.getProdStatus());
                            bProdStageInfo.setAuditStatus(entity.getAuditStatus());
                            bProdStageInfo.setCreateTime(lastUpddt);
                            bProdStageInfo.setOprId(user.getId());
                            bProdStageInfo.setLastUpdateTime(lastUpddt);
                            bProdStageInfo.setOprId(user.getId());
                            BeanUtils.copyProperties(bProdStageInfo, bProdStageDB);
                            bProdStageDB.setId(null);
                            bProdStageInfoService.save(bProdStageDB);
                        } else {

//                            this.detach(bProdStageDB);
//                            bProdStageDB.setFeeEffectiveDate(now);
//                            bProdStageDB.setStageStatus(entity.getProdStatus());
                            bProdStageDB.setAuditStatus(entity.getAuditStatus());
                            bProdStageInfo.setAuditStatus(entity.getAuditStatus());
                            bProdStageInfo.setFeeExpiryDate(BusinessContainer.doomsday);
                            bProdStageInfo.setCreateTime(lastUpddt);
                            bProdStageInfo.setStageStatus(bProdStageDB.getStageStatus());
                            bProdStageInfo.setOprId(user.getId());
                            bProdStageInfo.setLastUpdateTime(lastUpddt);
                            bProdStageInfo.setOprId(user.getId());
                            bProdStageInfoService.updateById(bProdStageDB);
                        }

                        //插入临时表
                        bizTmpService.saveBizTmp(bProdStageInfo, user, instId, BizOprEnum.OPR_UPDATE.getType(),DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN)
                                , oprMenuId, oprMenuName, entity.getProdStatus(), entity.getAuditStatus(), String.valueOf(bProdStageDB.getId()), webNextUrl, null, null, null);
                    }
                }

                List<BProdAuthCtrl> listDB = bProdAuthCtrlService.getBaseProdAuthCtrl(entity.getInstId(), ProdTypeEnum.BASE_PROD.getType(), entity.getBaseProdCode());
                List<BizTmp> listTmpDBDel = Lists.newArrayList();//待删除的数据，需要审核时只从临时表删除
                //查找提交数据中无，数据库有的数据
                for (BProdAuthCtrl bProdAuthCtrlDB : listDB) {
                    boolean del = true;
                    for (BProdAuthCtrl bProdAuthCtrl : entity.getBProdAuthCtrlList()) {
                        if (bProdAuthCtrlDB.getInstId().equals(bProdAuthCtrl.getInstId())
                                && bProdAuthCtrlDB.getProdType().equals(bProdAuthCtrl.getProdType())
                                && bProdAuthCtrlDB.getBaseProdCode().equals(bProdAuthCtrl.getBaseProdCode())
                                && bProdAuthCtrlDB.getProdCode().equals(bProdAuthCtrl.getProdCode())
                                && bProdAuthCtrlDB.getTxnNum().equals(bProdAuthCtrl.getTxnNum())
                                && bProdAuthCtrlDB.getBizType().equals(bProdAuthCtrl.getBizType())) {
                            del = false;
                        }
                    }
                    if (del) {//如果需要删除则从临时表取出待删除
                        BizTmp tmp = bizTmpService.getBizTmpDetail(bProdAuthCtrlDB.getId());
                        if (Common.isNotBlank(tmp)) {
                            listTmpDBDel.add(tmp);
                        }
                    }
                }
                if (listTmpDBDel.size() > 0) {//从临时表删除
                    bizTmpService.delete(listTmpDBDel);
                }

                for (BProdAuthCtrl bProdAuthCtrl : entity.getBProdAuthCtrlList()) {
                    BProdAuthCtrl bProdAuthCtrlDB =
                            bProdAuthCtrlService.findByBProdAuthCtrl(bProdAuthCtrl.getInstId(), bProdAuthCtrl.getProdType(), bProdAuthCtrl.getBaseProdCode(), bProdAuthCtrl.getTxnNum(), bProdAuthCtrl.getBizType());

                    if (Common.isBlank(bProdAuthCtrlDB)) {//如果数据库中查询不到则初始化
                        bProdAuthCtrlDB = new BProdAuthCtrl();
                        bProdAuthCtrl.setAuditStatus(entity.getAuditStatus());
                        bProdAuthCtrl.setCreateTime(lastUpddt);
                        bProdAuthCtrl.setOprId(user.getId());
                        bProdAuthCtrl.setLastUpdateTime(lastUpddt);
                        bProdAuthCtrl.setLastOprId(user.getId());
                        bProdAuthCtrl.setStatus(entity.getProdStatus());
                        BeanUtils.copyProperties(bProdAuthCtrl, bProdAuthCtrlDB);
                        bProdAuthCtrlDB.setId(null);
                        bProdAuthCtrlService.save(bProdAuthCtrlDB);
                    } else {
//                        this.detach(bProdAuthCtrlDB);
//                        bProdAuthCtrlDB.setStatus(entity.getProdStatus());
                        bProdAuthCtrlDB.setAuditStatus(entity.getAuditStatus());
                        bProdAuthCtrl.setStatus(bProdAuthCtrlDB.getStatus());
                        bProdAuthCtrl.setAuditStatus(entity.getAuditStatus());
                        bProdAuthCtrl.setCreateTime(lastUpddt);
                        bProdAuthCtrl.setLastUpdateTime(lastUpddt);
                        bProdAuthCtrl.setOprId(user.getId());
                        bProdAuthCtrl.setLastUpdateTime(lastUpddt);
                        bProdAuthCtrl.setLastUpdateTime(lastUpddt);
                        bProdAuthCtrl.setLastOprId(user.getId());
                        bProdAuthCtrlService.updateById(bProdAuthCtrlDB);
                    }

                    //插入临时表
                    bizTmpService.saveBizTmp(bProdAuthCtrlDB, user, instId, BizOprEnum.OPR_UPDATE.getType(),DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN)
                            , oprMenuId, oprMenuName, entity.getProdStatus(), entity.getAuditStatus(), String.valueOf(bProdAuthCtrlDB.getId()), webNextUrl, null, null, null);
                }
            } else {
                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                entity.setLastOprId(user.getId());
                entity.setLastUpdateTime(lastUpddt);
                bBaseProdInfo = this.getById(entity.getId());

                BeanUtils.copyProperties(entity, bBaseProdInfo);
                this.updateById(bBaseProdInfo);

                //历史表新增
                bizHisService.saveBizHisNew(bBaseProdInfo, bBaseProdInfo, user, instId, BizOprEnum.OPR_UPDATE.getType(),DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN)
                        , oprMenuId, oprMenuName, String.valueOf(bBaseProdInfo.getId()), null, null, null);


                String now = DateUtils.formatDate(new Date(), DateUtils.FORMAT_YYYYMMDD);
                if (!BaseProdCodeEnum.CREDIT_CARD_STAGE_PROD.getType().equals(entity.getBaseProdCode())) {//除了信用卡分期其他都单独设置费率
                    //界面操作时费率只有修改按钮没有删除按钮，不需要判断是否有需要删除的数据
                    for (BProdFeeInfo bProdFeeInfo : entity.getBProdFeeList()) {
//                        BProdFeeInfo bProdFeeDB =
//                                bProdFeeInfoService.getEffectProdFee(bProdFeeInfo.getInstId(), bProdFeeInfo.getProdType(), bProdFeeInfo.getBaseProdCode(), bProdFeeInfo.getProdCode(), bProdFeeInfo.getFeeType(), Integer.valueOf(now));
                      BProdFeeInfo bProdFeeDB = bProdFeeInfoService.getById(bProdFeeInfo.getId());
                        BProdFeeInfo bProdFeeDBOld = bProdFeeDB;
                        if (Common.isBlank(bProdFeeDB)) {//如果数据库中查询不到则初始化
                            bProdFeeDB = new BProdFeeInfo();
                            BeanUtils.copyProperties(bProdFeeInfo, bProdFeeDB);
                            bProdFeeDB.setId(null);
                            bProdFeeDB.setFeeEffectiveDate(now);
                            bProdFeeDB.setFeeExpiryDate(BusinessContainer.doomsday);
                            bProdFeeDB.setFeeStatus(entity.getProdStatus());
                            bProdFeeDB.setAuditStatus(entity.getAuditStatus());
                            bProdFeeDB.setCreateTime(lastUpddt);
                            bProdFeeDB.setLastOprId(user.getId());
                            bProdFeeDB.setLastUpdateTime(lastUpddt);
                            bProdFeeDB.setOprId(user.getId());
                            bProdFeeInfoService.convertFeeAmt(bProdFeeDB, bProdFeeInfo);
                            bProdFeeInfoService.save(bProdFeeDB);
                        } else {
                            bProdFeeDB.setId(bProdFeeInfo.getId());
                            bProdFeeDB.setFeeInputMode(bProdFeeInfo.getFeeInputMode());
                            bProdFeeDB.setFeeRate(bProdFeeInfo.getFeeRate());
                            bProdFeeInfoService.convertFeeAmt(bProdFeeDB, bProdFeeInfo);
//                            bProdFeeDB.setFeeEffectiveDate(String.valueOf(now));
                            bProdFeeDB.setFeeExpiryDate(BusinessContainer.doomsday);
                            bProdFeeDB.setFeeStatus(entity.getProdStatus());
                            bProdFeeDB.setAuditStatus(entity.getAuditStatus());
                            bProdFeeDB.setCreateTime(lastUpddt);
                            bProdFeeDB.setLastOprId(user.getId());
                            bProdFeeDB.setLastUpdateTime(lastUpddt);
                            bProdFeeDB.setOprId(user.getId());
                            bProdFeeInfoService.updateById(bProdFeeDB);
                        }
                        bizHisService.saveBizHisNew(bProdFeeDB, Common.isBlank(bProdFeeDBOld) ? bProdFeeDB : bProdFeeDBOld, user, instId, BizOprEnum.OPR_UPDATE.getType(),DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN)
                                , oprMenuId, oprMenuName, String.valueOf(bProdFeeDB.getId()), null, null, null);
                    }
                }
                //界面操作时分期有修改按钮、删除按钮，需要判断是否有需要删除的数据
                if (BaseProdCodeEnum.CREDIT_CARD_STAGE_PROD.getType().equals(entity.getBaseProdCode())) {//除了信用卡分期其他都单独设置费率
                    List<BProdStageInfo> listDB = bProdStageInfoService.getBaseProdStageList(entity.getInstId(), ProdTypeEnum.BASE_PROD.getType(), entity.getBaseProdCode());
                    List<BProdStageInfo> listDBDel = Lists.newArrayList();//待删除的数据，需要审核时只从临时表删除
                    //查找提交数据中无，数据库有的数据
                    for (BProdStageInfo bProdStageDB : listDB) {
                        boolean del = true;
                        for (BProdStageInfo bProdStageInfo : entity.getBProdStageList()) {
                            if (bProdStageDB.getId().equals(bProdStageInfo.getId())) {
                                del = false;
                            }
                        }
                        if (del) {//如果需要删除则从正式表表取出待删除
                            listDBDel.add(bProdStageDB);
                        }
                    }
                    if (listDBDel.size() > 0) {//从正式表删除
                        bProdStageInfoService.delete(listDBDel);
                    }
                    for (BProdStageInfo bProdStageInfo : entity.getBProdStageList()) {
                        BProdStageInfo bProdStageDB =
                                bProdStageInfoService.getfindbProdStageInfo(bProdStageInfo.getInstId(), bProdStageInfo.getProdType(), bProdStageInfo.getBaseProdCode(), bProdStageInfo.getProdStageCode(), bProdStageInfo.getProdStageNum());
                        BProdStageInfo bProdStageDBOld = bProdStageDB;
                        if (Common.isBlank(bProdStageDB)) {//如果数据库中查询不到则初始化
                            bProdStageDB = new BProdStageInfo();
                            BeanUtils.copyProperties(bProdStageInfo, bProdStageDB);
                            bProdStageDB.setId(null);
                            bProdStageDB.setFeeEffectiveDate(now);
                            bProdStageDB.setFeeExpiryDate(BusinessContainer.doomsday);
                            bProdStageDB.setStageStatus(entity.getProdStatus());
                            bProdStageDB.setAuditStatus(entity.getAuditStatus());
                            bProdStageDB.setCreateTime(lastUpddt);
                            bProdStageDB.setLastOprId(user.getId());
                            bProdStageDB.setLastUpdateTime(lastUpddt);
                            bProdStageDB.setOprId(user.getId());
                            bProdStageDB.setStagePrimaryKey(bProdStageDB.getProdType()
                                    + String.format("%2s", null == bProdStageInfo.getProdCode() ? "" : bProdStageInfo.getProdCode()) + bProdStageInfo.getBaseProdCode()
                                    + String.format("%32s", bProdStageDB.getProdStageCode())
                                    + String.format("%2s", bProdStageDB.getProdStageNum()) + bProdStageDB.getFeeEffectiveDate());
                            bProdStageInfoService.save(bProdStageDB);
                        } else {
                            bProdStageDB.setId(bProdStageDBOld.getId());
//                            bProdStageDB.setFeeEffectiveDate(String.valueOf(now));
                            bProdStageDB.setFeeExpiryDate(BusinessContainer.doomsday);
                            bProdStageDB.setStageStatus(entity.getProdStatus());
                            bProdStageDB.setAuditStatus(entity.getAuditStatus());
                            bProdStageDB.setCreateTime(bProdStageDBOld.getCreateTime());
                            bProdStageDB.setLastOprId(user.getId());
                            bProdStageDB.setLastUpdateTime(lastUpddt);
                            bProdStageDB.setOprId(bProdStageDBOld.getOprId());
                            bProdStageInfoService.updateById(bProdStageDB);
                        }

                        bizHisService.saveBizHisNew(bProdStageDB, Common.isBlank(bProdStageDBOld) ? bProdStageDB : bProdStageDBOld, user, instId, BizOprEnum.OPR_UPDATE.getType(),DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN)
                                , oprMenuId, oprMenuName, String.valueOf(bProdStageDB.getId()), null, null, null);

                    }
                }

                List<BProdAuthCtrl> listDB = bProdAuthCtrlService.getBaseProdAuthCtrl(entity.getInstId(), ProdTypeEnum.BASE_PROD.getType(), entity.getBaseProdCode());
                List<BProdAuthCtrl> listDBDel = Lists.newArrayList();//待删除的数据，需要审核时只从临时表删除
                //查找提交数据中无，数据库有的数据
                for (BProdAuthCtrl bProdAuthCtrlDB : listDB) {
                    boolean del = true;
                    for (BProdAuthCtrl bProdAuthCtrl : entity.getBProdAuthCtrlList()) {
                        if (bProdAuthCtrlDB.getInstId().equals(bProdAuthCtrl.getInstId())
                                && bProdAuthCtrlDB.getProdType().equals(bProdAuthCtrl.getProdType())
                                && bProdAuthCtrlDB.getBaseProdCode().equals(bProdAuthCtrl.getBaseProdCode())
//                                &&bProdAuthCtrlDB.getProdCode().equals(bProdAuthCtrl.getProdCode())
                                && bProdAuthCtrlDB.getTxnNum().equals(bProdAuthCtrl.getTxnNum())
                                && bProdAuthCtrlDB.getBizType().equals(bProdAuthCtrl.getBizType())) {
                            del = false;
                        }
                    }
                    if (del) {//如果需要删除则从正式表取出待删除
                        listDBDel.add(bProdAuthCtrlDB);
                    }
                }
                if (listDBDel.size() > 0) {//从正式表删除
                    bProdAuthCtrlService.delete(listDBDel);
                }

                for (BProdAuthCtrl bProdAuthCtrl : entity.getBProdAuthCtrlList()) {
                    BProdAuthCtrl bProdAuthCtrlDB =
                            bProdAuthCtrlService.findByBProdAuthCtrl(bProdAuthCtrl.getInstId(), bProdAuthCtrl.getProdType(), bProdAuthCtrl.getBaseProdCode(), bProdAuthCtrl.getTxnNum(), bProdAuthCtrl.getBizType());
                    BProdAuthCtrl bProdAuthCtrlDBOld = bProdAuthCtrlDB;
                    if (Common.isBlank(bProdAuthCtrlDB)) {//如果数据库中查询不到则初始化
                        bProdAuthCtrlDB = new BProdAuthCtrl();
                        BeanUtils.copyProperties(bProdAuthCtrl, bProdAuthCtrlDB);
                        bProdAuthCtrlDB.setId(null);
                        bProdAuthCtrlDB.setAuditStatus(entity.getAuditStatus());
                        bProdAuthCtrlDB.setStatus(entity.getProdStatus());
                        bProdAuthCtrlDB.setCreateTime(lastUpddt);
                        bProdAuthCtrlDB.setLastOprId(user.getId());
                        bProdAuthCtrlDB.setLastUpdateTime(lastUpddt);
                        bProdAuthCtrlDB.setOprId(user.getId());
                        bProdAuthCtrlService.save(bProdAuthCtrlDB);
                    } else {
                        bProdAuthCtrlDB.setId(entity.getId());
                        bProdAuthCtrlDB.setStatus(entity.getProdStatus());
                        bProdAuthCtrlDB.setAuditStatus(entity.getAuditStatus());
                        bProdAuthCtrlDB.setStatus(entity.getProdStatus());
                        bProdAuthCtrlDB.setCreateTime(bProdAuthCtrlDBOld.getCreateTime());
                        bProdAuthCtrlDB.setLastOprId(user.getId());
                        bProdAuthCtrlDB.setLastUpdateTime(lastUpddt);
                        bProdAuthCtrlDB.setOprId(bProdAuthCtrlDBOld.getOprId());
                        bProdAuthCtrlService.updateById(bProdAuthCtrlDB);
                    }
                    String id1 = bProdAuthCtrlDB.getId();
                    bizHisService.saveBizHisNew(bProdAuthCtrlDB, Common.isBlank(bProdAuthCtrlDBOld) ? bProdAuthCtrlDB : bProdAuthCtrlDBOld, user, instId, BizOprEnum.OPR_UPDATE.getType(),DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN)
                            , oprMenuId, oprMenuName, id1, null, null, null);
                }
            }
            //        }
        } else if (PageOptFlagEnum.OPT_FLAG_STOP.getType().equals(entity.getOptFlag())
                || PageOptFlagEnum.OPT_FLAG_USE.getType().equals(entity.getOptFlag())
                || PageOptFlagEnum.OPT_FLAG_CANCEL.getType().equals(entity.getOptFlag())) {
            UpmpPrivilegeCheck privilegeCheck = new UpmpPrivilegeCheck();
            String bizOpr = "";
            if (PageOptFlagEnum.OPT_FLAG_STOP.getType().equals(entity.getOptFlag())) {
                bizOpr = BizOprEnum.OPR_STOP.getType();
                privilegeCheck = privilegeCheckService.getPrivilegeCheckByPrivilegeCode(instId,
                        oprBtnNamePrefix + BusinessContainer.AUTH_PRIVILEGE_CODE_SPLIT + PageOptFlagEnum.OPT_FLAG_STOP.getDesc());
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    bBaseProdInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType());
                } else {
                    bBaseProdInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                    bBaseProdInfo.setProdStatus(BizDataStatusEnum.STATUS_STOPED.getType());
                }
            } else if (PageOptFlagEnum.OPT_FLAG_USE.getType().equals(entity.getOptFlag())) {

                bizOpr = BizOprEnum.OPR_USE.getType();
                privilegeCheck = privilegeCheckService.getPrivilegeCheckByPrivilegeCode(instId,
                        oprBtnNamePrefix + BusinessContainer.AUTH_PRIVILEGE_CODE_SPLIT + PageOptFlagEnum.OPT_FLAG_USE.getDesc());
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    bBaseProdInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType());
                } else {
                    bBaseProdInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                    bBaseProdInfo.setProdStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                }
            } else if (PageOptFlagEnum.OPT_FLAG_CANCEL.getType().equals(entity.getOptFlag())) {

                bizOpr = BizOprEnum.OPR_CANCEL.getType();
                privilegeCheck = privilegeCheckService.getPrivilegeCheckByPrivilegeCode(instId,
                        oprBtnNamePrefix + BusinessContainer.AUTH_PRIVILEGE_CODE_SPLIT + PageOptFlagEnum.OPT_FLAG_CANCEL.getDesc());
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    bBaseProdInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType());
                } else {
                    bBaseProdInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                    bBaseProdInfo.setProdStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
                }
            }
            bBaseProdInfo.setLastOprId(user.getId());
            //            bBaseProdInfo.setProdStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
            bBaseProdInfo.setLastUpdateTime(lastUpddt);
            this.updateById(bBaseProdInfo);

            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                //检查临时表是否有数据，如果没用则新增
                //                saveBizTmp(bBaseProdInfo,user,org,bizOpr,lastUpddt,oprMenuId,oprMenuName);

                bizTmpService.saveBizTmp(bBaseProdInfo, user, instId, bizOpr,DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN)
                        , oprMenuId, oprMenuName, bBaseProdInfo.getProdStatus(), bBaseProdInfo.getAuditStatus(), String.valueOf(bBaseProdInfo.getId()), webNextUrl, null, null, null);
            } else {
                //历史表新增
                //                saveBizHis(entity,user,org,bizOpr,lastUpddt,oprMenuId,oprMenuName);
                bizHisService.saveBizHisNew(bBaseProdInfo, bBaseProdInfo, user, instId, bizOpr,DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN)
                        , oprMenuId, oprMenuName, String.valueOf(bBaseProdInfo.getId()), null, null, null);
            }

            saveFollows(privilegeCheck, bBaseProdInfo, user, instId,DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN), oprMenuId,
                    oprMenuName, webNextUrl, bizOpr, entity.getOptFlag());

        } else if (PageOptFlagEnum.OPT_FLAG_CHECK.getType().equals(entity.getOptFlag())) {

            //获取临时表数据并转换成实体对象
            BizTmp tmp = bizTmpService.getById(bBaseProdInfo.getId());
            if (Common.isBlank(tmp)) {
                log.error("根据id没有找到指定临时表数据！");
                throw new RedpException("根据id没有找到指定临时表数据！");
            }
            String bizdata = tmp.getBizData1() + tmp.getBizData2() + tmp.getBizData3();
            BBaseProdInfo bProdInfoNew = JSON.parseObject(bizdata, BBaseProdInfo.class);

            String opt = entity.getOpt();
            if (Common.isBlank(opt)) {
                log.error("请求参数(opt)异常!");
                throw new RedpException("请求参数(opt)异常!");
            }

            String bizOpr = "";
            BeanUtils.copyProperties(bProdInfoNew, bBaseProdInfo);
//            String approvalRefuseReason = request.getParameter("approvalRefuseReason");
            if (BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType().equals(bBaseProdInfo.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    bBaseProdInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                    bBaseProdInfo.setProdStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                } else {
                    bBaseProdInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_ADD.getType();
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType().equals(bBaseProdInfo.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    bBaseProdInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                    bBaseProdInfo.setProdStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
                } else {
                    bBaseProdInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_CANCEL.getType();
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType().equals(bBaseProdInfo.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    bBaseProdInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                    bBaseProdInfo.setProdStatus(BizDataStatusEnum.STATUS_STOPED.getType());
                } else {
                    bBaseProdInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_STOP.getType();
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType().equals(bBaseProdInfo.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    bBaseProdInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                    bBaseProdInfo.setProdStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                } else {
                    bBaseProdInfo.setProdStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_USE.getType();
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType().equals(bBaseProdInfo.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    bBaseProdInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                    bBaseProdInfo.setProdStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                } else {
                    bBaseProdInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_UPDATE.getType();
            }
            this.updateById(bBaseProdInfo);
            if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                //审批通过将正式表更新到历史表
                bizHisService.saveBizHisNew(bBaseProdInfo, bBaseProdInfo, user, instId, bizOpr,DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN), oprMenuId, oprMenuName, String.valueOf(bBaseProdInfo.getId()), null, null, null);
            }
            // 修改临时表的审核状态等字段
            bizTmpService.saveBizTmp(bBaseProdInfo, user, instId, bizOpr,DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN), oprMenuId, oprMenuName, bBaseProdInfo.getProdStatus(), bBaseProdInfo.getAuditStatus(), String.valueOf(bBaseProdInfo.getId()), webNextUrl, null, null, null);

            saveFollows(null, bBaseProdInfo, user, instId,DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN), oprMenuId,
                    oprMenuName, webNextUrl, bizOpr, entity.getOptFlag());
        }
    }


    @Override
    public Map<String, Object> getEdit(String baseProdCode, String instId) throws RedpException {
        Map<String, Object> result = new HashMap<>();
        LambdaQueryWrapper<BBaseProdInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BBaseProdInfo::getInstId, instId);
        wrapper.eq(BBaseProdInfo::getBaseProdCode, baseProdCode);
        BBaseProdInfo entity = bBaseProdInfoService.getOne(wrapper);
        if (entity != null) {
            //基础产品费率
            List<BProdFeeInfo> feeInfos = bProdFeeInfoService.getBaseProdFeeList(instId, ProdTypeEnum.BASE_PROD.getType(), baseProdCode);

            //基础产品交易大类、交易(全量交易)
            List<PTxnGroup> txnGroups = pTxnCfgService.findPTxnGroupByList("", baseProdCode);
            List<BProdAuthCtrl> selectedTxnNums = bProdAuthCtrlService.getBaseProdAuthCtrl(instId, ProdTypeEnum.BASE_PROD.getType(), baseProdCode);

            if (null != txnGroups) {
                UpmpSysParam defaultInst = upmpSysParamService.getById(UpmpConstant.DEAFULT_INST_PARAM_ID);
                Map<String, UpmpSysParam> map = upmpSysParamService.findMapObjectByPrimaryKey(BusinessContainer.SYS_TXN_GROUP_PARAM, defaultInst.getPrimaryValue(), null);
                for (PTxnGroup pTxnGroup : txnGroups) {
                    if (map.get(pTxnGroup.getTxnGroup()) != null)
                        pTxnGroup.setTxnGroupName(map.get(pTxnGroup.getTxnGroup()).getPrimaryValue());
                    //获取当前交易大类下的所有交易小类
                    List<PTxnAuthMap> pTxnAuthMaps = pTxnGroup.getpTxnAuthMaps();
                    if (Common.isNotBlank(selectedTxnNums)) {
                        for (BProdAuthCtrl bMchntAuthCtrl : selectedTxnNums) {
                            if (pTxnGroup.getTxnGroup().equals(bMchntAuthCtrl.getTxnGroup())) {//如果当前记录的交易大类查出有值  则 对需要展示的交易大类 复选框勾上
                                pTxnGroup.setIsChecked(BusinessContainer.YES);
                            }
                            for (PTxnAuthMap pTxnAuthMap : pTxnAuthMaps) {
                                if (pTxnAuthMap.getTxnNum().equals(bMchntAuthCtrl.getTxnNum()) && pTxnAuthMap.getBizType().equals(bMchntAuthCtrl.getBizType())) {//如果此处的交易查出有值 则 对需要展示的交易 复选框勾上
                                    pTxnAuthMap.setIsChecked(BusinessContainer.YES);
                                    if (!BusinessContainer.YES.equals(pTxnGroup.getIsChecked())) {//如果交易被选中时，将交易大类选中
                                        pTxnGroup.setIsChecked(BusinessContainer.YES);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            List<BProdStageInfo> stageInfos = bProdStageInfoService.getBaseProdStageList(instId, ProdTypeEnum.BASE_PROD.getType(), baseProdCode);
            entity.setBProdStageList(stageInfos);
            entity.setBProdFeeList(feeInfos);
            result.put("pTxnGroups", txnGroups);
            result.put("feeInfos", feeInfos);
            result.put("stageInfos", stageInfos);
            result.put("entity", entity);
            result.put("optFlag", entity.getOptFlag());
        }
        return result;
    }


    /**
     * 保存BProdInfo附属关联表
     *
     * @param privilegeCheck
     * @param entity
     * @param user
     * @param instId
     * @param lastUpddt
     * @param oprMenuId
     * @param oprMenuName
     * @param webNextUrl
     * @param bizOpr
     */
    private void saveFollows(UpmpPrivilegeCheck privilegeCheck, BBaseProdInfo entity, UpmpUserInfoVO user, String instId, String lastUpddt, String oprMenuId,
                             String oprMenuName, String webNextUrl, String bizOpr, String optFlag) throws RedpException {
        if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {//需要审核

            int now = Integer.valueOf(DateUtils.formatDate(new Date(), DateUtils.FORMAT_YYYYMMDD));
            if (!BaseProdCodeEnum.CREDIT_CARD_STAGE_PROD.getType().equals(entity.getBaseProdCode())) {//除了信用卡分期其他都单独设置费率
                //先检查产品费率表中是否有标准产品的费率
                List<BProdFeeInfo> bProdFeeList = bProdFeeInfoService.selectBaseProdFeeList(entity.getInstId(), ProdTypeEnum.BASE_PROD.getType(), entity.getBaseProdCode());

                if (CollectionUtils.isNotEmpty(bProdFeeList)) {
                    for (BProdFeeInfo bProdFeeInfo : bProdFeeList) {
                        bProdFeeInfo.setFeeStatus(entity.getProdStatus());
                        bProdFeeInfo.setAuditStatus(entity.getAuditStatus());
                        bProdFeeInfo.setLastOprId(user.getId());
                        bProdFeeInfo.setLastUpdateTime(lastUpddt);
                        bizTmpService.saveBizTmp(bProdFeeInfo, user, instId, bizOpr,DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN)
                                , oprMenuId, oprMenuName, entity.getProdStatus(), entity.getAuditStatus(), String.valueOf(bProdFeeInfo.getId()), webNextUrl, null, null, null);

                    }
                }
            }
            if (BaseProdCodeEnum.CREDIT_CARD_STAGE_PROD.getType().equals(entity.getBaseProdCode())) {//信用卡分期产品
                List<BProdStageInfo> bProdStageList = bProdStageInfoService.selectBaseProdStageList(entity.getInstId(), ProdTypeEnum.BASE_PROD.getType(), entity.getBaseProdCode());

                if (CollectionUtils.isNotEmpty(bProdStageList)) {
                    for (BProdStageInfo bProdStageInfo : bProdStageList) {
                        bProdStageInfo.setStageStatus(entity.getProdStatus());
                        bProdStageInfo.setAuditStatus(entity.getAuditStatus());
                        bProdStageInfo.setLastOprId(user.getId());
                        bProdStageInfo.setLastUpdateTime(lastUpddt);
                        bizTmpService.saveBizTmp(bProdStageInfo, user, instId, bizOpr,DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN)
                                , oprMenuId, oprMenuName, entity.getProdStatus(), entity.getAuditStatus(), String.valueOf(bProdStageInfo.getId()), webNextUrl, null, null, null);

                    }
                }
            }

            List<BProdAuthCtrl> bProdAuthCtrlList = bProdAuthCtrlService.selectBaseProdAuthCtrl(entity.getInstId(), ProdTypeEnum.BASE_PROD.getType(), entity.getBaseProdCode());
            if (CollectionUtils.isNotEmpty(bProdAuthCtrlList)) {
                for (BProdAuthCtrl bProdAuthCtrl : bProdAuthCtrlList) {
                    bProdAuthCtrl.setStatus(entity.getProdStatus());
                    bProdAuthCtrl.setAuditStatus(entity.getAuditStatus());
                    bProdAuthCtrl.setLastOprId(user.getId());
                    bProdAuthCtrl.setLastUpdateTime(lastUpddt);
                    bizTmpService.saveBizTmp(bProdAuthCtrl, user, instId, bizOpr,DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN)
                            , oprMenuId, oprMenuName, entity.getProdStatus(), entity.getAuditStatus(), String.valueOf(bProdAuthCtrl.getId()), webNextUrl, null, null, null);
                }
            }
        } else {//不需要审核
            int now = Integer.valueOf(DateUtils.formatDate(new Date(), DateUtils.FORMAT_YYYYMMDD));
            if (!BaseProdCodeEnum.CREDIT_CARD_STAGE_PROD.getType().equals(entity.getBaseProdCode())) {//除了信用卡分期其他都单独设置费率//先检查产品费率表中是否有标准产品的费率
                List<BProdFeeInfo> bProdFeeList = bProdFeeInfoService.selectBaseProdFeeList(entity.getInstId(), ProdTypeEnum.BASE_PROD.getType(), entity.getBaseProdCode());

                if (CollectionUtils.isNotEmpty(bProdFeeList)) {
                    for (BProdFeeInfo bProdFeeInfo : bProdFeeList) {
                        BProdFeeInfo bProdFeeInfoOld = bProdFeeInfo;
                        bProdFeeInfo.setFeeStatus(entity.getProdStatus());
                        bProdFeeInfo.setAuditStatus(entity.getAuditStatus());
                        bProdFeeInfo.setLastOprId(user.getId());
                        bProdFeeInfo.setLastUpdateTime(lastUpddt);
                        bProdFeeInfoService.updateById(bProdFeeInfo);
                        bizHisService.saveBizHisNew(bProdFeeInfo, bProdFeeInfoOld, user, instId, bizOpr,DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN)
                                , oprMenuId, oprMenuName, bProdFeeInfo.getId(), null, null, null);

                    }
                }
            }
            if (BaseProdCodeEnum.CREDIT_CARD_STAGE_PROD.getType().equals(entity.getBaseProdCode())) {//信用卡分期产品
                List<BProdStageInfo> bProdStageList = bProdStageInfoService.selectBaseProdStageList(entity.getInstId(), ProdTypeEnum.BASE_PROD.getType(), entity.getBaseProdCode());

                if (CollectionUtils.isNotEmpty(bProdStageList)) {
                    for (BProdStageInfo bProdStageInfo : bProdStageList) {
                        BProdStageInfo bProdStageInfoOld = bProdStageInfo;
                        bProdStageInfo.setStageStatus(entity.getProdStatus());
                        bProdStageInfo.setAuditStatus(entity.getAuditStatus());
                        bProdStageInfo.setLastOprId(user.getId());
                        bProdStageInfo.setLastUpdateTime(lastUpddt);
                        bProdStageInfoService.updateById(bProdStageInfo);
                        bizHisService.saveBizHisNew(bProdStageInfo, bProdStageInfoOld, user, instId, bizOpr,DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN)
                                , oprMenuId, oprMenuName, bProdStageInfo.getId(), null, null, null);

                    }

                }
            }
            List<BProdAuthCtrl> bProdAuthCtrlList = bProdAuthCtrlService.selectBaseProdAuthCtrl(entity.getInstId(), ProdTypeEnum.BASE_PROD.getType(), entity.getBaseProdCode());
            if (CollectionUtils.isNotEmpty(bProdAuthCtrlList)) {
                for (BProdAuthCtrl bProdAuthCtrl : bProdAuthCtrlList) {
                    BProdAuthCtrl bProdAuthCtrlOld = bProdAuthCtrl;
                    bProdAuthCtrl.setStatus(entity.getProdStatus());
                    bProdAuthCtrl.setAuditStatus(entity.getAuditStatus());
                    bProdAuthCtrl.setLastOprId(user.getId());
                    bProdAuthCtrl.setLastUpdateTime(lastUpddt);
                    QueryWrapper queryWrapper=new QueryWrapper();
                    queryWrapper.eq("ID",entity.getId());
                    queryWrapper.eq("INST_ID",entity.getInstId());
                    bProdAuthCtrlService.update(bProdAuthCtrl,queryWrapper);
                    bizHisService.saveBizHisNew(bProdAuthCtrl, bProdAuthCtrlOld, user, instId, bizOpr,DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN)
                            , oprMenuId, oprMenuName, bProdAuthCtrl.getId(), null, null, null);
                }
            }
        }
    }

    @Override
    public BBaseProdInfo findByUK(String instId, String baseProdCode) {
        LambdaQueryWrapper<BBaseProdInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BBaseProdInfo::getInstId, instId);
        wrapper.eq(BBaseProdInfo::getBaseProdCode, baseProdCode);
        BBaseProdInfo info = this.getOne(wrapper);
        if (info == null) {
            UpmpSysParam defaultInst = upmpSysParamService.getById(UpmpConstant.DEAFULT_INST_PARAM_ID);
            wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BBaseProdInfo::getInstId, defaultInst.getPrimaryValue());
            wrapper.eq(BBaseProdInfo::getBaseProdCode, baseProdCode);
            info = this.getOne(wrapper);
        }
        return info;
    }
}
